//
//  JBBaseTableViewController.swift
//  sinaBlog
//
//  Created by jobo on 15/10/28.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

class JBBaseTableViewController: UITableViewController {

    var login = JBUserAccount.loadAccount() != nil
    override func loadView() {
        login ? super.loadView() : setupVisiterView()
        
    }
    
    func setupVisiterView()
    {
        let visitorView = JBVisitorView()
        view = visitorView
        
        
        //上方导航栏左右边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorLeftItem")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorRightItem")
        
        //设置上方导航按钮颜色
        UINavigationBar.appearance().tintColor  = UIColor.orangeColor()

        
        // 设置代理
        visitorView.visitorViewDelegate = self

        
        //view.backgroundColor = UIColor.whiteColor()
        view.backgroundColor = UIColor(colorLiteralRed: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        print(self)
        
        
        
        if self is JBHomeTableViewController
        {
             print("home")
             visitorView.startAnmation()
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
        }
        else if self is JBMessageTableViewController
        {
             print("message")
            visitorView.setVisitorView("visitordiscover_image_message", label: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }
        else if self is JBDiscoverTableViewController
        {
             print("discover")
            visitorView.setVisitorView("visitordiscover_image_message", label: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }
        else if self is JBProfileTableViewController
        {
             print("profile")
            visitorView.setVisitorView("visitordiscover_image_profile", label: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
    }
    //程序到后台暂停动画
    func didEnterBackground()
    {
        (view as! JBVisitorView).pauseAnimation()
    }
    //程序到前台继续动画
    func didBecomeActive()
    {
        (view as! JBVisitorView).resumeAnimation()
    }
    
    func visitorLeftItem()
    {
        print("左上角注册按钮")
    }
    func visitorRightItem()
    {
        print("右上角登录按钮")
    }
    
}
// MARK: - 扩展 CZBaseTableViewController 实现 CZVistorViewDelegte 协议
//相当于 category, 方便代码的管理
extension JBBaseTableViewController : JBVisitorVeiwDelegate
{
    // MARK: - 代理方法
    func reginClickDel() {
        print(__FUNCTION__)
    }
    
    func loginClickDel() {
        print(__FUNCTION__)
        
        let controller = JBOAuthViewController()
        
        presentViewController(UINavigationController(rootViewController: controller), animated: true, completion: nil)
        
    }
}
